package cn.newtouch.org.service.impl;

import cn.newtouch.basic.exception.BusinessException;
import cn.newtouch.basic.service.impl.BaseServiceImpl;
import cn.newtouch.basic.util.BaiduAuditUtils;
import cn.newtouch.org.domain.Employee;
import cn.newtouch.org.domain.Shop;
import cn.newtouch.org.domain.ShopLog;
import cn.newtouch.org.dto.ShopDto;
import cn.newtouch.org.mapper.EmployeeMapper;
import cn.newtouch.org.mapper.ShopLogMapper;
import cn.newtouch.org.mapper.ShopMapper;
import cn.newtouch.org.service.IShopService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List;

// @Transactional(readOnly = true,propagation = Propagation.SUPPORTS) 继承性
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopLogMapper shopLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    //具体如何入驻以及需要验证的操作
    @Override
    public void settlement(Shop shop) {

        // 1.每个参数的空值判断
        if(StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getTel())
                || StringUtils.isEmpty(shop.getAddress())

                || StringUtils.isEmpty(shop.getAdmin().getUsername())
                || StringUtils.isEmpty(shop.getAdmin().getPhone())
                || StringUtils.isEmpty(shop.getAdmin().getEmail())
                || StringUtils.isEmpty(shop.getAdmin().getPassword())
                || StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())
        ){
            throw new BusinessException("参数不能为空!");
        }

        //调用百度审核
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if(!aBoolean){
            throw new BusinessException("店铺名称不合法，请重新输入！");
        }



        // 2.两次密码不一致校验
        if(!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new BusinessException("两次密码不一致!");
        }

        // 3.通过名字和地址查询该店铺是否已经被入驻过 这个方法需要mapper.xml写出来
        Shop shopTemp = shopMapper.getShopByMsg(shop);
        if(shopTemp != null){
            throw new BusinessException("店铺已经入驻，请直接登陆");
        }

        // 4.通过传来的shop拿到employee类型的admin
        Employee admin = shop.getAdmin();
        //把admin添加到employee中 返回自增主键
        employeeMapper.save(admin);

        // 5.通过admin拿到id ，shop把这个id 赋值给admin_id字段
        shop.setAdmin_id(admin.getId());
        // 把这个shop添加到表里面 并且返回自增主键
        shopMapper.save(shop);

        // 6.admin把shop返回的自增主键id赋值给字段shop_id
        admin.setShop_id(shop.getId());
        //把有shop_id值的admin  更新（修改）到 employee表
        employeeMapper.update(admin);



    }

    /**
     * 审核通过
     * @param log
     */
    @Override
    public void pass(ShopLog log) throws MessagingException {
        //1.修改状态：state = 2
        //通过参数里面的店铺id 查询到店铺
        Shop shop = shopMapper.loadById(log.getShop_id());
        //该店铺的状态
        shop.setState(2);
        //更新到数据库
        shopMapper.update(shop);

        //2.添加审核日志到数据库
        //编辑参数里面的状态
        log.setState(2);
        //编辑审核人
        log.setAudit_id(1L);
        //将参数添加到数据库表中
        shopLogMapper.save(log);

        //3.发送邮件：审核通过邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
        //设置发送方
        helper.setFrom("1713722063@qq.com");
        //设置主题
        helper.setSubject("店铺激活邮件");
        //设置内容
        helper.setText("<h1>你的店铺已经注册!!!</h1>" +
                "<a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);

        //获取收件人
        //通过参数里面的Admin_id获取到员工id 再通过员工调用根据id查询员工方法
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        helper.setTo(employee.getEmail());
        helper.setTo("1713722063@qq.com");
        //发送
        javaMailSender.send(mimeMessage);
    }

    /**
     * 驳回
     * @param log
     */
    @Override
    public void reject(ShopLog log) throws MessagingException {
        //1.修改状态：state = 4
        //通过参数里面的店铺id 查询到店铺
        Shop shop = shopMapper.loadById(log.getShop_id());
        //该店铺的状态
        shop.setState(4);
        //更新到数据库
        shopMapper.update(shop);

        //2.添加审核日志到数据库
        //编辑参数里面的状态
        log.setState(4);
        //编辑审核人
        log.setAudit_id(1L);
        //将参数添加到数据库表中
        shopLogMapper.save(log);

        //3.发送邮件：审核通过邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"UTF-8");
        //设置发送方
        helper.setFrom("1713722063@qq.com");
        //设置主题
        helper.setSubject("审核驳回邮件");
        //设置内容
        helper.setText("<h1>入驻失败!!!</h1>" +
                "<a href='http://localhost:8081/#/register'>点击重新入驻</a>",true);
        //获取收件人
        //通过参数里面的Admin_id获取到员工id 再通过员工调用根据id查询员工方法
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        helper.setTo(employee.getEmail());
        helper.setTo("1713722063@qq.com");
        //发送
        javaMailSender.send(mimeMessage);

        //4.删除入驻信息
        //删除管理员信息
        employeeMapper.remove(shop.getAdmin_id());
        //删除店铺信息
        shopMapper.remove(shop.getId());

    }

    @Override
    public List<ShopDto> getEcharts() {
        return shopMapper.getEcharts();
    }
}
